<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    var numMatching = function (s, words) {
      //记录words[i] 中是s的子序列的单词个数 
      let res = 0;
      let a = s;
      for (let i = 0; i < words.length; i++) {
        //题目要求不改变其余字符的相对顺序，所以不需要排序
        let va = words[i]
        for (let j = 0; j < va.length; j++) {
          //数组中元素的字符存在s中，则截取存在元素后面的字符串继续进行判断
          if (s.indexOf(va[j]) > -1) {
            s = s.substr(s.indexOf(va[j]) + 1)
          } else {//如果不存在，跳出此次循环  
            break;
          }
          //执行到此处，数组中元素的每一个字符都进行了对比，都在s中存在
          if (j === words[i].length - 1) {
            res++
          }
        }
        //复原s，进行新一轮的对比
        s = a;
      }
      return res
    };
    console.log(numMatching(s = "abcde", words = ["a", "bb", "acd", "ace"]))
  </script>
</body>

</html>